package com.example.koma.myedittext;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;

/**
 * Created by koma on 2017/6/14.
 */

public class MyEditText extends RelativeLayout{


    private EditText et;
    private ImageView iv;

    private String hintText;
    private int inputType;

    public MyEditText(Context context, AttributeSet attrs){
        super(context, attrs);

        LayoutInflater.from(context).inflate(R.layout.layout_et, this, true);
        et = (EditText) findViewById(R.id.et);
        iv = (ImageView) findViewById(R.id.iv);

        initParams(context, attrs);
    }


    private void initParams(Context context, AttributeSet attrs){

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyEditText);

        if(typedArray != null){

            hintText = typedArray.getString(R.styleable.MyEditText_hint_text);
            inputType = typedArray.getInt(R.styleable.MyEditText_input_type, 0);

            setHintText(hintText);
            setInputType(inputType);

        }


        et.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {

                String s = editable.toString();
                if(TextUtils.isEmpty(s)){
                    iv.setVisibility(View.GONE);
                }else{
                    iv.setVisibility(View.VISIBLE);
                }

            }
        });

        iv.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                et.setText("");
            }
        });


    }


    public void setHintText(String hint){
        if(!TextUtils.isEmpty(hint)){
            et.setHint(hint);
        }
    }


    public void setInputType(int type){
        Log.e("111","type-->"+type);
        switch (type){
            case 0:
                et.setInputType(InputType.TYPE_CLASS_TEXT);
                break;
            case 1:
                et.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                break;
            case 2:
                et.setInputType(InputType.TYPE_CLASS_PHONE);
                break;
            case 3:
                et.setInputType(InputType.TYPE_CLASS_NUMBER);
                break;
        }

    }

    public String getText(){
        return et.getText().toString().trim();
    }


}
